package cn.gjxblog.modules.job.controller;

import cn.gjxblog.common.annotation.SysLog;
import cn.gjxblog.common.model.HttpResult;
import cn.gjxblog.common.model.PageInfo;
import cn.gjxblog.common.model.PageRequest;
import cn.gjxblog.modules.job.entity.ScheduleJobEntity;
import cn.gjxblog.modules.job.service.ScheduleJobService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;






/**
 * 定时任务
 *
 * @author guojiaxun
 * @date 2019-09-18 13:53:04
 */
@RestController
@RequestMapping("sys/schedulejob")
public class ScheduleJobController {
    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("获取列表")
    public HttpResult<PageInfo> list(@RequestBody PageRequest pageRequest){
        PageInfo pageInfo = scheduleJobService.searchList(pageRequest);
        return HttpResult.ok(pageInfo);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{jobId}")
    @ApiOperation("获取信息")
    public HttpResult info(@PathVariable("jobId") Long jobId){
		ScheduleJobEntity scheduleJob = scheduleJobService.getById(jobId);

        return HttpResult.ok(scheduleJob);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存信息")
    public HttpResult save(@RequestBody ScheduleJobEntity scheduleJob){
		scheduleJobService.save(scheduleJob);

        return HttpResult.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改信息")
    public HttpResult update(@RequestBody ScheduleJobEntity scheduleJob){
		scheduleJobService.updateById(scheduleJob);

        return HttpResult.ok();
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    @ApiOperation("删除信息")
    public HttpResult delete(@RequestBody Long[] jobIds){
		scheduleJobService.removeByIds(Arrays.asList(jobIds));
        return HttpResult.ok();
    }

    /**
     * 立即执行任务
     */
    @SysLog("立即执行任务")
    @GetMapping("/run")
    @ApiOperation("执行任务")
    public HttpResult run(Long[] jobIds){
        scheduleJobService.run(jobIds);

        return HttpResult.ok();
    }

}
